/*
	Mining Laser Ent for McBuilds' Mining
	
	Author: Steeveeo
*/

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('entities/base_wire_entity/init.lua'); 
include("shared.lua");

//Variable Definitions
ENT.EnergyUsage = 500
ENT.ResourceReturn = 500
ENT.RefireRate = 1
ENT.Asteroids = {}

function ENT:Initialize()
	
	self.BaseClass.Initialize(self)
	
	//Model and Physics Setup
	self.Entity:SetModel("models/Slyfo_2/acc_sci_coolerator.mdl")
	self.Entity:SetName( "Personal Ore Receiver" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS ) 	
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS ) 

	//Physics Init
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Sleep()
		phys:EnableGravity( true )
		phys:EnableCollisions( true )
		phys:EnableMotion( true )
		phys:SetMass( 300 )
	end
	
	//RD3 Stuff
	self:AddResource("energy", 0)
	//--Replace the following with real mining resources when we get that far
	self:AddResource("Ore", 0)
	self:AddResource("Crystals", 0)
	
	//Wire IO
	if not (WireAddon == nil) then
		self.Outputs = WireLib.CreateSpecialOutputs(self.Entity, { "Active", "EnergyUsage", "Ore", "Crystals", "Rubble", "Asteroid" }, {"NORMAL", "NORMAL", "NORMAL", "NORMAL", "NORMAL", "ARRAY"} )
	end
end

function ENT:Think()

	//Reset Wire
	Wire_TriggerOutput(self.Entity, "Ore", 0)
	Wire_TriggerOutput(self.Entity, "Crystals", 0)
	Wire_TriggerOutput(self.Entity, "Rubble", 0)
	Wire_TriggerOutput(self.Entity, "Active", 0)
	Wire_TriggerOutput(self.Entity, "Asteroid", nil)
	
	self.mult = 0

	//Is this thing on?
	
	
	for k,Active in pairs(self.ActiveMiners) do
		
		self.mult = self.mult + Active
		self.EnergySupply = self:GetResourceAmount("energy") > (self.EnergyUsage * self.mult)
	
		if ( Active >= 1 ) then
	
			Wire_TriggerOutput(self.Entity, "Active", 1)
			//Check if we have enough energy
			if self:GetResourceAmount("energy") < (self.EnergyUsage * self.mult) then
				self.Active = 0
				self:SetOOO(0)
				Wire_TriggerOutput(self.Entity, "EnergyUsage", 0)
			else
			
				//It drains energy
				self:SetOOO(1)
				self:ConsumeResource("energy", self.EnergyUsage)
				Wire_TriggerOutput(self.Entity, "EnergyUsage", self.EnergyUsage)
				
				//An Entity is present
			
				if ValidEntity(self.Ent) then
					Wire_TriggerOutput(self.Entity, "Asteroid", self.Ent)
				
					//Is it a roid?
					if self.Asteroids[k]:GetClass() == "mining_asteroid" then
						//Mine the 'Roid!
						local Receive = self.Asteroids[k]:MiningSequence(self.ResourceReturn)
						
						//Give Returns
						//--These are all placeholders for the real thing, replace when possible!
						//--Ore
						if Receive["Ore"] > 0 then
						self:SupplyResource("Ore",Receive["Ore"])
						end
						//--Crystals
						if Receive["Crystals"] > 0 then
							self:SupplyResource("Crystals",Receive["Crystals"])
						end
						//Report to Wire
						Wire_TriggerOutput(self.Entity, "Ore", Receive["Ore"])
						Wire_TriggerOutput(self.Entity, "Crystals", Receive["Crystals"])
						Wire_TriggerOutput(self.Entity, "Rubble", Receive["Rubble"])
					end
				end
			end
		end
	end
	self.Entity:NextThink(CurTime() + self.RefireRate)
	return true
end
